import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/api/odoo/sale_bid_result_manufactor_odoo_api.dart';
import 'package:flutter_admin/models/odoo/sale_bid_result_manufactor.dart';
import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_input.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import '../../../generated/l10n.dart';

class SaleBidResultManufactorEdit extends StatefulWidget {

  String resultId;
  SaleBidResultManufactorEdit({this.saleBidResultManufactor,required this.resultId});

  final SaleBidResultManufactor? saleBidResultManufactor;

  @override
  _SaleBidResultManufactorEditState createState() =>
      _SaleBidResultManufactorEditState();
}

class _SaleBidResultManufactorEditState
    extends State<SaleBidResultManufactorEdit> {
  late SaleBidResultManufactor saleBidResultManufactor;
  bool? isAdd;

  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  ValueNotifier<num> bidamount = ValueNotifier<num>(0);
  ValueNotifier<num> bidresult = ValueNotifier<num>(0);

  @override
  void initState() {
    super.initState();
    this.isAdd = widget.saleBidResultManufactor == null;
    this.saleBidResultManufactor =
        widget.saleBidResultManufactor ?? SaleBidResultManufactor();

    // this.saleBidResultManufactor.resultId =
    //     widget.saleBidResultManufactor!.resultId;
    _loadSelectVO();
  }

  _loadSelectVO() async {
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CryInput(
            label: '*参与厂家',
            value: saleBidResultManufactor.bidmanufactor,
            onSaved: (v) {
              saleBidResultManufactor.bidmanufactor = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInputNum(
            label: '*投标报价',
            value: saleBidResultManufactor.tenderOffer,
            onSaved: (v) {
              saleBidResultManufactor.tenderOffer = v;
            },
            onChange: (v) {
              formKey.currentState?.save();
            },
            validator: (v) {
              return v!.isEmpty
                  ? S.of(context).required
                  : v == '0'
                      ? '投标报价不能为0'
                      : null;
            },
          ),
          CryInputNum(
            label: '*价格得分',
            value: saleBidResultManufactor.priceScore,
            onSaved: (v) {
              saleBidResultManufactor.priceScore = v;
            },
            onChange: (v) {
              formKey.currentState?.save();
            },
            validator: (v) {
              return v!.isEmpty
                  ? S.of(context).required
                  : v == '0'
                      ? '价格得分不能为0'
                      : null;
            },
          ),
          CryInputNum(
            label: '*中标名次',
            value: saleBidResultManufactor.bidaranking,
            onSaved: (v) {
              saleBidResultManufactor.bidaranking = v;
            },
            onChange: (v) {
              formKey.currentState?.save();
            },
            validator: (v) {
              return v!.isEmpty
                  ? S.of(context).required
                  : v == '0'
                      ? '中标名次不能为0'
                      : null;
            },
          ),
          CryInputNum(
            label: '*中标金额',
            value: saleBidResultManufactor.bidamount,
            onSaved: (v) {
              saleBidResultManufactor.bidamount = v;
            },
            onChange: (v) {
              formKey.currentState?.save();
            },
            validator: (v) {
              return v!.isEmpty
                  ? S.of(context).required
                  : v == '0'
                      ? '中标金额不能为0'
                      : null;
            },
          ),
          CryInputNum(
            label: '*中标份额（%）',
            value: saleBidResultManufactor.bidshare,
            onSaved: (v) {
              saleBidResultManufactor.bidshare = v;
            },
            onChange: (v) {
              formKey.currentState?.save();
            },
            validator: (v) {
              return v!.isEmpty
                  ? S.of(context).required
                  : v == '0'
                      ? '中标份额（%）不能为0'
                      : null;
            },
          ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: S.of(context).save,
          onPressed: () {
            FormState form = formKey.currentState!;
            if (!form.validate()) {
              return;
            }
            form.save();
            saleBidResultManufactor.resultId = widget.resultId;
            SaleBidResultManufactorOdooApi.saveOrUpdate(
                    saleBidResultManufactor.toOdooMap())
                .then((ResponseBodyApi res) {
              if (!res.success!) {
                //package:flutter_admin/pages/common/cry(re s.message!);
                return;
              }
              Navigator.pop(context, true);
              CryUtils.message(S.of(context).saved);
            });
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text(this.isAdd! ? S.of(context).add : S.of(context).modify),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
